package com.wzh.glsc.product.app;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wzh.common.utils.PageUtils;
import com.wzh.common.utils.R;
import com.wzh.glsc.product.entity.BrandEntity;
import com.wzh.glsc.product.entity.CategoryBrandRelationEntity;
import com.wzh.glsc.product.service.CategoryBrandRelationService;
import com.wzh.glsc.product.vo.BrandVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * 品牌分类关联
 *
 * @author lcm
 * @email 1291529969@gmail.com
 * @date 2022-03-18 05:33:17
 */
@RestController
@RequestMapping("product/categorybrandrelation")
public class CategoryBrandRelationController {
    @Autowired
    private CategoryBrandRelationService categoryBrandRelationService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = categoryBrandRelationService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
		CategoryBrandRelationEntity categoryBrandRelation = categoryBrandRelationService.getById(id);

        return R.ok().put("categoryBrandRelation", categoryBrandRelation);
    }

    /**
     * 信息
     * /product/categorybrandrelation/brands/list
     */
    @RequestMapping("/brands/list")
    public R getBrandsListByCatId(@RequestParam(value = "catId",required = true) Long catId){
        List<BrandEntity> brandEntities = categoryBrandRelationService.getBrandsListByCatId(catId);
        List<BrandVo> brandVoList=brandEntities.stream().map((tempObj)->{
            BrandVo brandVo=new BrandVo();
            brandVo.setBrandId(tempObj.getBrandId());
            brandVo.setBrandName(tempObj.getName());
            return brandVo;
        }).collect(Collectors.toList());
        return R.ok().put("data", brandVoList);
    }

    /**
     * 列表
     */
    @GetMapping("/catelog/list")
    public R catelogList(@RequestParam("brandId")Long brandId){
        List<CategoryBrandRelationEntity> data = categoryBrandRelationService.list(
                new QueryWrapper<CategoryBrandRelationEntity>().eq("brand_id",brandId));
        return R.ok().put("data", data);
    }

//    /**
//     * 信息
//     */
//    @GetMapping("/brands/list")
//    public R brandsList(@PathVariable("catId") Long catId){
//        CategoryBrandRelationEntity categoryBrandRelation = categoryBrandRelationService.getById(catId);
//
//        return R.ok().put("categoryBrandRelation", categoryBrandRelation);
//    }


    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody CategoryBrandRelationEntity categoryBrandRelation){
//		categoryBrandRelationService.save(categoryBrandRelation);
        categoryBrandRelationService.saveDetail(categoryBrandRelation);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody CategoryBrandRelationEntity categoryBrandRelation){
		categoryBrandRelationService.updateById(categoryBrandRelation);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids){
		categoryBrandRelationService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
